<template>
  <div class="setMeal" style="height: 100%">
    <div class="pageTitleSubTitle">
      <div class="title">套餐列表</div>
      <div class="btn">
        <a-button type="primary" @click="openModal">导入</a-button>
        <a-button type="primary" @click="openModal">导出</a-button>
        <a-button v-if="configJurisdiction.add" type="primary" @click="openModal"><a-icon type="plus" />添加套餐</a-button>
      </div>
    </div>
    <Search :search="search" @searchVal="searchVal"/>
    <div class="storeContent">
      <a-table :columns="columns" :data-source="columnsData" :pagination="false" :rowKey="(record,index) => index">
        <span slot="area" slot-scope="area,record">{{area.name?area.name:'--'}}</span>
        <span slot="store" slot-scope="store">{{store && store.name?store.name:'--'}}</span>
        <span slot="area_no" slot-scope="area_no,record" class="operation">
          <span v-if="configJurisdiction.update" @click="editColumns(record,1)">修改</span>
          <span v-if="configJurisdiction.del" @click="removeStore(record)">{{record.status != 1?'上架':'下架'}}</span>
          <span v-if="configJurisdiction.look" @click="editColumns(record,2)">查看</span>
        </span>
      </a-table>
      <Pagination :current="page" :total="total" @pageSize="pageSize" />
    </div>
  </div>
</template>
<script>
  import Pagination from '@/common/pagination/index'
  import Search from '@/common/search/index'
  import {column} from "./index";
  export default {
    name:'setMeal',
    components:{Pagination,Search},
    data(){
      return {
        page:1,
        total:0,
        columns:[],
        columnsData:[],
        searchText:{},
        search:[
          {
            type:'input',
            name:'套餐名称',
            place:'请输入名称',
            val:'',
            key:'name',
          },
          {
            type:'cascader',
            name:'套餐分类',
            place:'请选择分类',
            val:[],
            key:['cate_id','cate_sub_id'],
            selectData:[]
          }
        ],
        jurisdiction:[
          { key:'admin-package-detail', back:'look'},
          { key:'admin-package-add', back:'add'},
          { key:'admin-package-update', back:'update'},
          { key:'admin-package-drop-off', back:'del'},
        ],
        configJurisdiction:{}
      }
    },
    created() {
      this.columns = column;
      this.getList();
      this.getCates();
      this.configJurisdiction = this.getJurisdiction(2,this.jurisdiction)
    },
    methods:{
      openModal(){
        this.$router.push({path:'/admin-package-add',query:{type:1}})
      },
      editColumns(item,type){
        this.$router.push({path:'/admin-package-add',query:{id:item.good_no,type:2}});
      },
      removeStore(e){
        this.Api.statusPackages({status:e.status == 1?2:1},e.good_no).then(res=>{
          if(res.code !== 200) return this.$message.info(res.msg);
          this.$message.info(res.msg);
          this.getList()
        })
      },
      searchVal(e){
        this.searchText = e;
        this.getList()
      },
      pageSize(e){
        this.page = e;
        this.getList()
      },
      getList(){
        let data = this.searchText;
        data.size = 10;
        data.page = this.page;
        let info = JSON.parse(sessionStorage.getItem('search'));
        let configData = {...data,...info};
        this.Api.packages(configData).then(res=>{
          if(res.code !== 200) return;
          this.total = res.data.total;
          this.columnsData = res.data.items
        })
      },
      getCates(){
        this.Api.cates({type:2}).then(res=>{
          let data = res.data;
          data.forEach(item=>{
            item.label = item.name;
            item.value = item.id;
            item.children = item.child;
            item.children.forEach(demo=>{
              demo.label = demo.name;
              demo.value = demo.id;
              demo.children = demo.child;
              if(demo.children && demo.children.length<=0) delete demo.children
            })
          });
          this.search.forEach(item => {
            if (item.type === 'cascader') item.selectData = data
          });
          // appendData.forEach(item=>{
          //   if(item.type === 'cascader') item.selectData = data
          // });
        })
      },
    }
  }
</script>
